﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Lotour.Search.Common
{
    public class StringBoy
    {
        /// <summary>
        /// 清除 input 中的html标记
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Obsolete("建议用 StringBoy.ClearGarbage(String input,String []patterns) 方法替代")]
        public static String ClearHtml(String input)
        {
            if (String.IsNullOrEmpty(input))
            {
                return "";
            }

            String pattern = @"<[^>]+>";
            Regex reg = new Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            input = reg.Replace(input, new MatchEvaluator(OnMatch));
            return input;
        }
        /// <summary>
        /// 清除那些正则表达式匹配到的垃圾字符串!!! 
        /// </summary>
        /// <param name="input">源字符串</param>
        /// <param name="regs">正则表达式数组</param>
        /// <returns></returns>
        public static String ClearGarbage(String input, String[] regs)
        {
            if (String.IsNullOrEmpty(input))
            {
                return "";
            }
            if (regs.Length > 0)
            {
                StringBuilder patterns = new StringBuilder(512);
                {
                    foreach (String reg in regs)
                    {
                        patterns.Append("(" + reg + ")|");
                    }
                }
                Regex r = new Regex(patterns.ToString());
                input = r.Replace(input, new MatchEvaluator(OnMatch));
            }
            return input;
        }

        /// <summary>
        /// 从输入字符串中按字节截取字符串,此方法适用于截取少量的字符串
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="byteCount"></param>
        /// <returns></returns>
        public static String GetStringByBytes(String input, int start, int byteCount)
        {
            try
            {
                input = input.Substring(start);
            }
            catch (Exception e)
            {
                return "";
            }
            StringBuilder part = new StringBuilder(512);
            for (int i = 0; i < input.Length; i++)
            {
                if (Encoding.UTF8.GetBytes(part.ToString()).Length < byteCount)
                {
                    part.Append(input.Substring(i, 1));
                }
                else
                {
                    break;
                }
            }
            return part.ToString();
        }

        public static String CutStringByBytes(String input, int needByteCount, Encoding en)
        {
            #region//验证
            if (String.IsNullOrEmpty(input))
            {
                return "";
            }
            if (needByteCount < 1)
            {
                return "";
            }
            if (en == null)
            {
                en = System.Text.Encoding.UTF8;
            }
            #endregion
            #region
            ////字符串的总字节数
            int bcount = en.GetByteCount(input);
            if (needByteCount <= 0 || bcount <= needByteCount)
            {
                return input;
            }
            int cutPoint = GetCutPoint(input, 0, 1, needByteCount, en);
            return input.Substring(0, cutPoint);
            #endregion
        }

        private static int GetCutPoint(String input, int cutPoint, int power, int needByteCount, Encoding en)
        {
            if (cutPoint == 0)
            {
                cutPoint = (int)(Math.Ceiling(input.Length * (double)Math.Pow(0.5, 1)));
            }
            if (cutPoint > input.Length)
            {
                cutPoint = input.Length - 1;
            }
            String halfString = input.Substring(0, cutPoint);
            int halfByteCount = en.GetByteCount(halfString);
            if (needByteCount - halfByteCount < -1)
            {
                input = halfString;
                return GetCutPoint(input, 0, power, needByteCount, en);
            }
            else if (needByteCount - halfByteCount > 1)
            {
                cutPoint += (int)(Math.Ceiling(input.Length * (double)Math.Pow(0.5, ++power)));
                return GetCutPoint(input, cutPoint, power, needByteCount,en);
            }
            else
            {
                return cutPoint;
            }
        }

        private static String OnMatch(Match m)
        {
            return "";
        }
    }
}
